package org.jeecg.common.util;

import org.jeecg.common.system.vo.TreeNode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 树节点工具
 * @author mzz
 */
public class TreeNodeUtil {

    public static List<TreeNode>  generalTreeList(Collection<TreeNode> treeNodes){
        List<TreeNode> treeList = new ArrayList<>();
        assembleTreeNode(treeList,treeNodes,null);
        return treeList;
    }


    /**
     * 组装树
     */
    private static void assembleTreeNode(List<TreeNode> treeList, Collection<TreeNode> metaList, TreeNode parentNode) {
        for (TreeNode tree : metaList) {
            String parentKey = tree.getParentKey();
            if (parentNode == null && oConvertUtils.isEmpty(parentKey)) {
                treeList.add(tree);
                if (!tree.getIsLeaf()) {
                    assembleTreeNode(treeList, metaList, tree);
                }
            } else if (parentNode != null && parentKey != null && parentKey.equals(parentNode.getKey())) {
                parentNode.getChildren().add(tree);
                if (!tree.getIsLeaf()) {
                    assembleTreeNode(treeList, metaList, tree);
                }
            }
        }
    }
}
